import math
import matplotlib.pyplot as plt
import numpy as np

title_size = 18
label_size = 16

def relu(x):
  return x if x > 0 else 0

def leaky_relu(x, lamb):
  return x if x >0 else (lamb * x)

def RReLU(x):
  return x if x > 0 else (np.random.rand() * 0.5 * x)

def Noisy_ReLU(x):
  a = x + np.random.randn() * 0.1
  return relu(a)

x = [a/float(100) for a in range(-121,121,4)]
y_1 = [relu(a) for a in x]
y_2 = [leaky_relu(a, 0.4) for a in x]
y_3 = [RReLU(a) for a in x]
y_4 = [Noisy_ReLU(a) for a in x]


fig = plt.figure()
plt.subplots_adjust(hspace=0.4,wspace=0.3)
ax1 = fig.add_subplot(2,2,1)
ax1.plot(x,y_1)
ax1.set_title(u"ReLU", fontsize=title_size)
ax1.set_xlabel(u"x", fontsize=label_size)
ax1.set_ylabel(u"f(x)", fontsize=label_size)


ax2 = fig.add_subplot(2,2,2)
ax2.plot(x,y_2)
ax2.set_title(u"Leaky_ReLU", fontsize=title_size)
ax2.set_xlabel(u"x", fontsize=label_size)
ax2.set_ylabel(u"f(x)", fontsize=label_size)


ax3 = fig.add_subplot(2,2,3)
ax3.plot(x,y_3)
ax3.set_title(u"RReLU", fontsize=title_size)
ax3.set_xlabel(u"x", fontsize=label_size)
ax3.set_ylabel(u"f(x)", fontsize=label_size)


ax4 = fig.add_subplot(2,2,4)
ax4.plot(x,y_4)
ax4.set_title(u"Noisy_ReLU", fontsize=title_size)
ax4.set_xlabel(u"x", fontsize=label_size)
ax4.set_ylabel(u"f(x)", fontsize=label_size)


plt.ylim(-0.5,1.2)

# plt.show()
plt.savefig('relu.png')
